﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccess;
using Entities;
using MySql.Data.MySqlClient;
using System.IO;
namespace BusinessFacade
{
    public class Busi_sys_Menu
    {
        public Busi_sys_Menu()
        {

        }
        public static DataTable Getlist()
        {
            DataSet ds = DbHelperMySql.Query("select * from sys_menu where MenuParentNo=0");
            return ds.Tables[0];
        }
        public static string GetModel(int id)
        {
            DataSet ds = DbHelperMySql.Query("select * from sys_menu where MenuParentNo=0 and MenuID=" + id);
            if(ds.Tables[0].Rows.Count<=0)
            {
                return "";
            }
            return ds.Tables[0].Rows[0]["MenuName"].ToString();
        }
        public static void DeleteMenu(int id)
        {
            MySqlParameter[] parameters = { 
                             new MySqlParameter("pRightID",MySqlDbType.Int32,11)             
                                          };
            parameters[0].Value = id;
            DbHelperMySql.RunProcedure("sys_roleright_Delete", parameters);
        }
        public static bool Delete(int id)
        {
            int rows = DbHelperMySql.ExecuteSql("Delete from sys_menu where MenuID=" + id);
            if (rows > 0)
            {
                return true;
            }
            return false;
        }

        public static bool Add(sys_menu_Model model)
        {
            int rows;
            MySqlParameter[] parameters = {
					new MySqlParameter("pMenuID", MySqlDbType.Int32,11),
					new MySqlParameter("pMenuParentNo", MySqlDbType.Int32,11),
					new MySqlParameter("pMenuName", MySqlDbType.VarChar,500),};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.MenuParentNo;
            parameters[2].Value = model.MenuName;
            DbHelperMySql.RunProcedure("sys_menu_ADD", parameters);
            rows = int.Parse(parameters[0].Value.ToString());
            if(rows>0)
            {
                return true;
            }
            return false;
        }
        public static bool AddMenu(sys_roleright_Model model)
        {
            int rows;
            MySqlParameter[] parameters = {
					new MySqlParameter("pRightID", MySqlDbType.Int32,11),
					new MySqlParameter("pRoleID", MySqlDbType.Int32,11),
					new MySqlParameter("pMenuID", MySqlDbType.VarChar,500),};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.RoleID;
            parameters[2].Value = model.MenuID;
            DbHelperMySql.RunProcedure("sys_roleright_ADD", parameters);
            rows = int.Parse(parameters[0].Value.ToString());
            if (rows > 0)
            {
                return true;
            }
            return false;
        }
        public static void UpdateMenu(sys_roleright_Model model)
        {
            MySqlParameter[] parameters = {
					new MySqlParameter("pRightID", MySqlDbType.Int32,11),
					new MySqlParameter("pRoleID", MySqlDbType.Int32,11),
					new MySqlParameter("pMenuID", MySqlDbType.VarChar,500),};
            parameters[0].Value = model.RightID;
            parameters[1].Value = model.RoleID;
            parameters[2].Value = model.MenuID;
            DbHelperMySql.RunProcedure("sys_roleright_Update", parameters);
        }
        public static DataTable GetMenuFunctionModel(int pUserID)
        {
            MySqlParameter[] parameters = {
					new MySqlParameter("pRightID", MySqlDbType.Int32,11)};
            parameters[0].Value = pUserID;
            return DbHelperMySql.RunProcedure("sys_roleright_GetModel", parameters).Tables[0];
        }
        public static DataTable GetMenuFunctionList(int rightID)
        {
            MySqlParameter[] parameters = {
					new MySqlParameter("pUserID", MySqlDbType.Int32,11)};
            parameters[0].Value = rightID;
            return DbHelperMySql.RunProcedure("sys_roleright_GetList", parameters).Tables[0];
        }
        public static void Update(sys_menu_Model model)
        {
            MySqlParameter[] parameters = {
					new MySqlParameter("pMenuID", MySqlDbType.Int32,11),
					new MySqlParameter("pMenuParentNo", MySqlDbType.Int32,11),
					new MySqlParameter("pMenuName", MySqlDbType.VarChar,500),};
            parameters[0].Value = model.MenuID;
            parameters[1].Value = model.MenuParentNo;
            parameters[2].Value = model.MenuName;
            DbHelperMySql.RunProcedureNull("sys_menu_Update", parameters);
        }
        public static DataTable Getlist(int id)
        {
            MySqlParameter[] parameters = { 
                       new MySqlParameter("Id",MySqlDbType.Int32,11)
                                          };
            parameters[0].Value=id;
            DataSet ds = DbHelperMySql.RunProcedure("GetMenuList", parameters);
            return ds.Tables[0];
        }
        public sys_menu_Model GetModel1(int ID)
        {
            DataSet ds = new DataSet();
            sys_menu_Model model = new sys_menu_Model();
            ds = DbHelperMySql.Query("select * from sys_menu where MenuID = " + ID);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["MenuID"] != null && ds.Tables[0].Rows[0]["MenuID"].ToString() != "")
                {
                    model.MenuID = int.Parse(ds.Tables[0].Rows[0]["MenuID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MenuNo"] != null && ds.Tables[0].Rows[0]["MenuNo"].ToString() != "")
                {
                    model.MenuNo = int.Parse(ds.Tables[0].Rows[0]["MenuNo"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MenuParentNo"] != null && ds.Tables[0].Rows[0]["MenuParentNo"].ToString() != "")
                {
                    model.MenuParentNo = int.Parse(ds.Tables[0].Rows[0]["MenuParentNo"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MenuOrder"] != null && ds.Tables[0].Rows[0]["MenuOrder"].ToString() != "")
                {
                    model.MenuOrder = int.Parse(ds.Tables[0].Rows[0]["MenuOrder"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MenuName"] != null && ds.Tables[0].Rows[0]["MenuName"].ToString() != "")
                {
                    model.MenuName = ds.Tables[0].Rows[0]["MenuName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MenuUrl"] != null && ds.Tables[0].Rows[0]["MenuUrl"].ToString() != "")
                {
                    model.MenuUrl = ds.Tables[0].Rows[0]["MenuUrl"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MenuIcon"] != null && ds.Tables[0].Rows[0]["MenuIcon"].ToString() != "")
                {
                    model.MenuIcon = ds.Tables[0].Rows[0]["MenuIcon"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsVisible"] != null && ds.Tables[0].Rows[0]["IsVisible"].ToString() != "")
                {
                    model.IsVisible = int.Parse(ds.Tables[0].Rows[0]["IsVisible"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsLeaf"] != null && ds.Tables[0].Rows[0]["IsLeaf"].ToString() != "")
                {
                    model.IsLeaf = int.Parse(ds.Tables[0].Rows[0]["IsLeaf"].ToString());
                }
               
            }
            return model;
        }
        public static bool Add1(sys_menu_Model model)
        {
            int rows;
            MySqlParameter[] parameters = {
					new MySqlParameter("MenuID", MySqlDbType.Int32,11),
					new MySqlParameter("MenuParentNo", MySqlDbType.Int32,11),
					new MySqlParameter("MenuName", MySqlDbType.VarChar,500),
					new MySqlParameter("MenuUrl", MySqlDbType.VarChar,500)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.MenuParentNo;
            parameters[2].Value = model.MenuName;
            parameters[3].Value = model.MenuUrl;
            DbHelperMySql.RunProcedure("AddMenu", parameters);
            rows = int.Parse(parameters[0].Value.ToString());
            if (rows > 0)
            {
                return true;
            }
            return false;
        }
        public static void Update1(sys_menu_Model model)
        {
            MySqlParameter[] parameters = {
					new MySqlParameter("pMenuID", MySqlDbType.Int32,11),
					new MySqlParameter("MenuParentNo", MySqlDbType.Int32,11),
					new MySqlParameter("MenuName", MySqlDbType.VarChar,500),
					new MySqlParameter("MenuUrl", MySqlDbType.VarChar,500)};
            parameters[0].Value = model.MenuID;
            parameters[1].Value = model.MenuParentNo;
            parameters[2].Value = model.MenuName;
            parameters[3].Value = model.MenuUrl;
            DbHelperMySql.RunProcedure("UpdateMenu", parameters);
        }
        public DataTable GetFunctionmenu(int UserID)
        {
            MySqlParameter[] parameters = {
					new MySqlParameter("pUserID", MySqlDbType.Int32,11)};
            parameters[0].Value = UserID;
            return DbHelperMySql.RunProcedure("GetFunctionRole", parameters).Tables[0];
        }
        public DataTable GetTwoFunctionmenu(int UserID, int MenuID)
        {
            MySqlParameter[] parameters = {
					new MySqlParameter("pUserID", MySqlDbType.Int32,11),
                     new MySqlParameter("pMenuParentNo", MySqlDbType.Int32,11)};
            parameters[0].Value = UserID;
            parameters[1].Value = MenuID;
            return DbHelperMySql.RunProcedure("GetTwoFunctionRole", parameters).Tables[0];
        }

        public DataTable GetEditMain(int id) 
        {
            DataSet ds = DbHelperMySql.Query("select * from sys_menu where MenuID="+id+"");
            return ds.Tables[0];
        }
        public string GetUrl(int id) 
        {
            DataSet ds = DbHelperMySql.Query("select MenuUrl from sys_menu where MenuID=" + id);
            if (ds.Tables[0].Rows.Count <= 0)
            {
                return "";
            }
            return ds.Tables[0].Rows[0]["MenuUrl"].ToString();
        }
        public string GetName(int id)
        {
            DataSet ds = DbHelperMySql.Query("select MenuName from sys_menu where MenuID=" + id);
            if (ds.Tables[0].Rows.Count <= 0)
            {
                return "";
            }
            return ds.Tables[0].Rows[0]["MenuName"].ToString();
        }
        public DataTable GetClassName(int id)
        {
            DataSet ds = DbHelperMySql.Query("select  MenuParentNO from sys_menu where MenuID="+ id);
            return ds.Tables[0];
        }
        public DataTable GetBing(int id) 
        {
            DataSet ds = DbHelperMySql.Query("select * from sys_menu where MenuID=" + id);
            return ds.Tables[0];
        }
    }
}
